package com.jzl.swing.ui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.jzl.swing.entity.Supplier;
import com.jzl.swing.service.SupplierService;
import com.jzl.swing.service.impl.SupplierServiceImpl;

import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AddSupplierUI {

	private JFrame frame;
	private JTextField textFieldSupplierName;
	private JTextField textFieldContact;
	private JTextField textFieldPhone;
	private JTextField textFieldAddr;
	private SupplierService supplierService = new SupplierServiceImpl();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddSupplierUI window = new AddSupplierUI();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public AddSupplierUI() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 583);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel label = new JLabel("供应商名称：");
		label.setBounds(88, 56, 85, 16);
		frame.getContentPane().add(label);
		
		textFieldSupplierName = new JTextField();
		textFieldSupplierName.setBounds(196, 51, 130, 26);
		frame.getContentPane().add(textFieldSupplierName);
		textFieldSupplierName.setColumns(10);
		
		JLabel label_1 = new JLabel("供应商描述：");
		label_1.setBounds(88, 132, 85, 16);
		frame.getContentPane().add(label_1);
		
		JTextArea textAreaSupplierDesc = new JTextArea();
		textAreaSupplierDesc.setBounds(196, 132, 178, 97);
		frame.getContentPane().add(textAreaSupplierDesc);
		
		JLabel label_2 = new JLabel("供应商联系人：");
		label_2.setBounds(88, 272, 96, 16);
		frame.getContentPane().add(label_2);
		
		textFieldContact = new JTextField();
		textFieldContact.setBounds(196, 267, 130, 26);
		frame.getContentPane().add(textFieldContact);
		textFieldContact.setColumns(10);
		
		JLabel label_3 = new JLabel("供应商电话：");
		label_3.setBounds(88, 341, 85, 16);
		frame.getContentPane().add(label_3);
		
		textFieldPhone = new JTextField();
		textFieldPhone.setBounds(196, 336, 130, 26);
		frame.getContentPane().add(textFieldPhone);
		textFieldPhone.setColumns(10);
		
		JLabel label_4 = new JLabel("供应商地址：");
		label_4.setBounds(88, 404, 85, 16);
		frame.getContentPane().add(label_4);
		
		textFieldAddr = new JTextField();
		textFieldAddr.setBounds(196, 399, 195, 26);
		frame.getContentPane().add(textFieldAddr);
		textFieldAddr.setColumns(10);
		
		JButton button = new JButton("添加");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String supplier_name = textFieldSupplierName.getText();
				String supplier_disc = textAreaSupplierDesc.getText();
				String supplier_phone = textFieldPhone.getText();
				String supplier_address = textFieldAddr.getText();
				String supplier_contacter = textFieldContact.getText();
				Supplier supplier = new Supplier();
				supplier.setSupplier_name(supplier_name);
				supplier.setsupplier_address(supplier_address);
				supplier.setsupplier_contacter(supplier_contacter);
				supplier.setsupplier_describ(supplier_disc);
				supplier.setsupplier_phone(supplier_phone);
				try {
					int i = supplierService.addSupplier(supplier);
					if (i>0) {
						JOptionPane.showMessageDialog(frame, "插入成功");
						frame.dispose();
					}
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
		button.setBounds(88, 496, 117, 29);
		frame.getContentPane().add(button);
		
		JButton button_1 = new JButton("关闭");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int i = JOptionPane.showConfirmDialog(frame, "是否确认关闭，您填写的信息将不会被保存");
				if (i==0) {
					frame.dispose();
				}
			}
		});
		button_1.setBounds(257, 496, 117, 29);
		frame.getContentPane().add(button_1);
	}
}
